package org.micode.demo.db.service.impl;

import org.micode.demo.db.consts.GlobalConsts;
import org.micode.demo.db.dao.SysMenuDao;
import org.micode.demo.db.dao.SysRoleMenuDao;
import org.micode.demo.db.domain.dto.SysMenuDTO;
import org.micode.demo.db.domain.entity.SysMenuDO;
import org.micode.demo.db.domain.query.SysMenuQuery;
import org.micode.demo.db.enums.MenuTypeEnum;
import org.micode.demo.db.service.DaoSysMenuService;
import org.micode.common.domain.vo.EditObjVO;
import org.micode.libs.dao.base.BaseAllServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 菜单信息(sys_menu) 服务实现类
 *
 * author: wutianbin
 */
@Service
@Slf4j
public class DaoSysMenuServiceImpl
        extends BaseAllServiceImpl<Long, SysMenuDTO, SysMenuDO, SysMenuQuery, SysMenuDao>
        implements DaoSysMenuService {

    @Autowired
    private SysMenuDao sysMenuDao;

    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;

    @Override
    protected SysMenuDao getMyDao() {
        return sysMenuDao;
    }

    @Override
    public List<SysMenuDTO> listMenuByUser(Long userId) {
        //系统管理员，拥有最高权限
        if(userId == GlobalConsts.SUPER_ADMIN){
            return getAllMenuList(null);
        }

        //用户菜单列表
        List<Long> menuIdList = sysMenuDao.queryUserAllMenuId(userId);
        return getAllMenuList(menuIdList);
    }

    @Override
    public List<SysMenuDTO> listMenuNotButtonByUser(Long userId) {
        //用户菜单列表
        List<SysMenuDTO> menuIdList;
        if (null != userId) {
            menuIdList = sysMenuDao.listUserMenuNotButton(userId);
        } else {
            menuIdList = sysMenuDao.listAllNotButton();
        }
        return menuIdList;
    }

    private List<SysMenuDTO> getAllMenuList(List<Long> menuIdList) {
        //查询根菜单列表
        List<SysMenuDTO> menuList = queryListParentId(0L, menuIdList);
        //递归获取子菜单
        getMenuTreeList(menuList, menuIdList);

        return menuList;
    }

    private List<SysMenuDTO> getMenuTreeList(List<SysMenuDTO> menuList, List<Long> menuIdList) {
        List<SysMenuDTO> subMenuList = new ArrayList<>();

        for(SysMenuDTO entity : menuList){
            //目录
            if(entity.getMenuType() == MenuTypeEnum.CATALOG.getValue()){
                entity.setList(getMenuTreeList(queryListParentId(entity.getMenuId(), menuIdList), menuIdList));
            }
            subMenuList.add(entity);
        }

        return subMenuList;
    }

    private List<SysMenuDTO> queryListParentId(Long parentId) {
        return this.sysMenuDao.queryListParentId(parentId);
    }

    private List<SysMenuDTO> queryListParentId(long parentId, List<Long> menuIdList) {
        List<SysMenuDTO> menuList = queryListParentId(parentId);
        if(menuIdList == null){
            return menuList;
        }

        List<SysMenuDTO> userMenuList = new ArrayList<>();
        for(SysMenuDTO menu : menuList){
            if(menuIdList.contains(menu.getMenuId())){
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    @Override
    public Set<String> listPerms(Long userId) {
        return sysMenuDao.listPerms(userId);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public int deleteByKeys(EditObjVO<List<Long>> keys) {
        int ret = super.deleteByKeys(keys);

        // 删除对应的角色权限

        for (Long menuId : keys.getWantEdit()) {
            sysRoleMenuDao.deleteForMenuId(menuId);
        }

        return ret;
    }

}
